package android_serialport_api;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SerialPort {

    private static final String TAG = "SerialPort";

    static {
        System.loadLibrary("serial_port");
    }

    private FileInputStream mFileInputStream;
    private FileOutputStream mFileOutputStream;

    public SerialPort(File device, int baudrate) throws SecurityException, IOException {
        FileDescriptor fd = open(device.getAbsolutePath(), baudrate, 0);
        if (fd == null) {
            throw new IOException("Serial port open failed!");
        }
        mFileInputStream = new FileInputStream(fd);
        mFileOutputStream = new FileOutputStream(fd);
    }

    private static native FileDescriptor open(String paramString, int paramInt1, int paramInt2);

    public InputStream getInputStream() {
        return mFileInputStream;
    }

    public OutputStream getOutputStream() {
        return mFileOutputStream;
    }

    public native void close();
}
